package com.zucc.core.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ViewResolver;

import com.mysql.fabric.xmlrpc.base.Data;
import com.sun.deploy.uitoolkit.impl.fx.Utils;
import com.sun.jmx.snmp.Timestamp;
import com.zucc.common.utils.Page;
import com.zucc.common.utils.Today;
import com.zucc.core.po.Equipment;
import com.zucc.core.po.MeetingRoom;
import com.zucc.core.po.MrCount;
import com.zucc.core.po.Reservation;
import com.zucc.core.service.EquipmentService;
import com.zucc.core.service.MeetingRoomService;
import com.zucc.core.service.ReserService;

@RequestMapping(value="/mr")
@Controller
public class MeetingRoomController {	
	
	private List<MeetingRoom> MR;
	
	@Autowired
	private MeetingRoomService mroomService;
	@Autowired
	private EquipmentService equipmentService;
	@Autowired
	private ReserService reserService;
	
	public void initState() {
		List<Integer> mrids=mroomService.findMRID(null, null, null);
		List<MeetingRoom> ck=new ArrayList<MeetingRoom>();
		for(int i=0;i<mrids.size();i++) {
			int id=mrids.get(i);
			MeetingRoom meetingRoom=new MeetingRoom();
			meetingRoom.setM_R_id(id);
			int c=mroomService.checkMRState(id,(new Today()).today());
			mroomService.updateMRState(id,c);
		}
	}
	
	@RequestMapping(value="/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,@RequestParam(defaultValue="5")Integer rows,String mName,String mState,String mSize,Model model,HttpServletRequest request) {
		List<String> mrns=mroomService.findMRName(mName, mState, mSize);
		List<Integer> mrids=mroomService.findMRID(mName, mState, mSize);
		this.initState();
		List<String> mrss=mroomService.findMRState( mName, mState, mSize);
		List<String> mrsis=mroomService.findMRSize(mName, mState, mSize);
		List<MeetingRoom> row2=new ArrayList<MeetingRoom>();
		for(int i=0;i<mrns.size();i++) {
			MeetingRoom meetingRoom=new MeetingRoom();
			meetingRoom.setM_R_id(mrids.get(i));
			meetingRoom.setM_Name(mrns.get(i));
			meetingRoom.setM_State(mrss.get(i));
			meetingRoom.setM_Size(mrsis.get(i));
			row2.add(meetingRoom);
		}	
		MR=row2;
		model.addAttribute("page",row2);
		model.addAttribute("mName", mName);
		model.addAttribute("mState", mState);
		model.addAttribute("mSize", mSize);	
		
		List<Equipment> equipments=equipmentService.selectEquip();
		model.addAttribute("equs", equipments);
		
		return "managerMain";
	}
	@RequestMapping("/create.action")
	@ResponseBody
	public String mrCreate(MeetingRoom meetingRoom,HttpSession session,HttpServletRequest request) {
		
		meetingRoom.setM_State("0");
		List<Integer> mrids=mroomService.findMRID(null,null,null);
		int id=mrids.get(mrids.size()-1)+1;
		meetingRoom.setM_R_id(id);
		String[] favorites;
		favorites = request.getParameterValues("equ");
		String sr=null;
		for(String s:favorites) {
			if (sr==null) {
				sr=s+";";
			}
			else {
				sr=sr+s+";";
			}
		}
		meetingRoom.setM_EquList(sr);
		int rows=mroomService.createMR(meetingRoom);
		if(rows>0) {
			return "OK";
			
		}
		else {
			return "FAIL";
		}
		
	}
	@RequestMapping("/delete.action")
	@ResponseBody
	public String mrDelete(Integer id) {
		
		int rows=mroomService.deleteMR(id);
		
		if(rows>0) {
			return "OK";
		}
		else {
			return "FAIL";
		}
		
	}
	
	@RequestMapping("/getMRById.action")
	@ResponseBody
	public MeetingRoom getMRById(Integer id){
		
		List<Integer> mrids=mroomService.findMRID(null,null,null);
		List<String> mrns=mroomService.findMRName(null,null,null);
		List<String> mrsis=mroomService.findMRSize(null,null,null);
		int index=0;
		for(Integer s:mrids) {
			index++;
			if(s==id) 
				break;
		}
		MeetingRoom result=new MeetingRoom();
		result.setM_Name(mrns.get(index-1));
		result.setM_R_id(id);
		result.setM_Size(mrsis.get(index-1));
		return result;
	}
	
		
		
	@RequestMapping("/updateMR.action")
	@ResponseBody
	public String mrUpdate(MeetingRoom meetingRoom) {
		System.out.println(meetingRoom);
		int rows=mroomService.updateMR(meetingRoom);
		if(rows>0)
			return "OK";
		else
			return "FAIL";
	}
	
	@RequestMapping("/viewUse.action")
	public String viewUse(Model model,HttpServletRequest request) {
		String today=(new Today()).today();
		List<MrCount> mrCounts=new ArrayList<>();
		for(MeetingRoom meetingRoom:MR) {
			MrCount mrCount=new MrCount();
			mrCount.setmId(meetingRoom.getM_R_id());
			mrCount.setmName(meetingRoom.getM_Name());
			int count=reserService.selectMRCount(meetingRoom.getM_R_id());
			mrCount.setCount(count);
			List<Integer> index=reserService.selectMRTimeCount(meetingRoom.getM_R_id(), today);
			int sum=0;
			for(int i:index) {
				sum+=i;
			}
			mrCount.setTimCount(sum);
			mrCounts.add(mrCount);
		}
		System.out.println(mrCounts);
		model.addAttribute("usrCount",mrCounts);
		return "use";
	}
	
}
